package kr.hyosang.android.penfo;

import java.util.List;

import kr.hyosang.android.penfo.parser.LoginParser;
import kr.hyosang.android.penfo.processor.BaseProcessor;
import kr.hyosang.android.penfo.processor.ProcessorListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity implements ProcessorListener<String[]>
{
    BaseProcessor<Void, String[]> mProcessor;
    
    EditText mLoginID;
    EditText mLoginPassword;
    Button mLoginButton;
    Button mLoginNoButton;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.login);
        
        mLoginID = (EditText)findViewById(R.id.loginId);
        mLoginPassword = (EditText)findViewById(R.id.loginPassword);
        mLoginButton = (Button)findViewById(R.id.loginButton);
        mLoginNoButton = (Button)findViewById(R.id.loginNoButton);
        
        mLoginButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0)
            {
                String id = mLoginID.getText().toString();
                String pw = mLoginPassword.getText().toString();
                
                if("".equals(id) || "".equals(pw)) {
                    //TODO no login info
                    return;
                }
                
                mProcessor = new BaseProcessor<Void, String[]>();
                mProcessor.setListener(LoginActivity.this);
                mProcessor.setParser(new LoginParser());
                mProcessor.addPostValue("user_id", id);
                mProcessor.addPostValue("password", pw);
                mProcessor.addPostValue("s_url", "/");
                mProcessor.addPostValue("auto_login", "1");
                mProcessor.execute(Definition.URL.LOGIN);
            }
        });
        
        
        //선택적 로그인으로 변경하면서 버튼 숨김처리.
        mLoginNoButton.setVisibility(View.GONE);
        /*
        mLoginNoButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                setResult(Definition.RESULT_FAIL);
                finish();
            }
        });
        */
    }

    @Override
    public void onComplete(String[] result)
    {
        if("OK".equals(result[0])) {
            List<String> cookie = mProcessor.getHeaderFields("Set-Cookie");
            SharedObject shared = SharedObject.getInstance();
            
            for(String ck : cookie) {
                String val;
                
                if(ck.startsWith("PHPSESSID=")) {
                    val = ck.substring(ck.indexOf('=')+1, ck.indexOf(';'));
                    shared.setPHPSESSID(val);
                }else if(ck.startsWith("ZBSESSIONID=")) {
                    val = ck.substring(ck.indexOf('=')+1, ck.indexOf(';'));
                    shared.setZBSESSIONID(val);
                }
            }
            
            
            setResult(Definition.RESULT_OK);
            
            Log.d("penfo", "로그인 성공");
            Log.d("penfo", "Cookie : " + cookie);
        }else {
            setResult(Definition.RESULT_FAIL);
            Log.d("penfo", "로그인 실패 : " + result[1]);
        }
        
        finish();
        
    }

}
